# class base for controller
from google.appengine.ext import webapp

# grandmonde import
from tools import yams_manager

class Index(webapp.RequestHandler):
    '''
    '''
    def get(self):
        '''
        '''
        # get the language id
        locale_id = self.request.get('language')
        # get the size name
        size = self.request.get('size')

        #
        ya_manager = yams_manager.YamsManager(
                locale_id=locale_id, size=size)

        #
        template = ya_manager.player()
        return self.response.out.write(template)

class Player(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id, ya_manager.size.width))

        #
        player_name = self.request.get('player_name')
        #
        player_key = ya_manager.new_player(player_name)

        if player_key:
            #
            create_game = self.request.get('create_game')
            choose_game = self.request.get('choose_game')

            #
            if not create_game == '':
                template = ya_manager.create_game(player_key)
                return self.response.out.write(template)

            #
            if not choose_game == '':
                template = ya_manager.choose_game(player_key)
                return self.response.out.write(template)

        # default! player already exist
        return self.redirect('/yams?language=%s&size=%s' % (
                    ya_manager.language.locale_id, ya_manager.size.width))

class DeletePlayer(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id,
                        ya_manager.size.width))

        #
        player_key = self.request.get('player_key')
        #
        ya_manager.delete_player(player_key)

        #
        return self.redirect('/yams?language=%s&size=%s' % (
                    ya_manager.language.locale_id,
                    ya_manager.size.width))

class CreateGame(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id, ya_manager.size.width))

        #
        player_key = self.request.get('player_key')
        #
        game_name = self.request.get('game_name')
        #
        if ya_manager.create(player_key, game_name):
            #
            ya_manager.init_score(player_key)
            ya_manager.init_dices(player_key)
            #
            template = ya_manager.show(player_key)
            return self.response.out.write(template)

        #
        return self.redirect('/yams?language=%s&size=%s' % (
            ya_manager.language.locale_id, ya_manager.size.width))

class ChooseGame(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id,
                        ya_manager.size.width))

        #
        player_key = self.request.get('player_key')
        #
        game_name = self.request.get('game_name')
        game_key = self.request.get('game_key')
        #
        ya_manager.choose(player_key, game_key)
        #
        ya_manager.init_score(player_key)
        ya_manager.init_dices(player_key)

        #
        template = ya_manager.show(player_key)
        return self.response.out.write(template)

class DeleteGame(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id,
                        ya_manager.size.width))

        #
        game_key = self.request.get('game_key')
        #
        ya_manager.delete_game(game_key)

        #
        return self.redirect('/yams?language=%s&size=%s' % (
                    ya_manager.language.locale_id,
                    ya_manager.size.width))

class Roll(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id,
                        ya_manager.size.width))

        #
        player_key = self.request.get('player_key')

        #
        dice1 = self.request.get('dice1')
        #
        dice2 = self.request.get('dice2')
        #
        dice3 = self.request.get('dice3')
        #
        dice4 = self.request.get('dice4')
        #
        dice5 = self.request.get('dice5')

        #
        new_turn = ya_manager.roll(
                player_key,
                dice1,
                dice2,
                dice3,
                dice4,
                dice5)

        #
        template = ya_manager.show(player_key, new_turn=new_turn)
        return self.response.out.write(template)

class Score(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id,
                        ya_manager.size.width))

        #
        player_key = self.request.get('player_key')

        #
        score_name = self.request.get('score_name')

        #
        if not self.request.get('select') == '':
            #
            score_value = self.request.get('score_value')

        else:
            score_value = 0

        #
        ya_manager.update_score(player_key, score_name, score_value)

        #
        ya_manager.init_dices(player_key)

        #
        new_turn = True

        #
        template = ya_manager.show(player_key, new_turn=new_turn)
        return self.response.out.write(template)

class ShowGame(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        ya_manager = yams_manager.YamsManager(
                language_key=language_key, size_key=size_key)

        #
        if not self.request.get('cancel') == '':
            return self.redirect('/yams?language=%s&size=%s' % (
                        ya_manager.language.locale_id,
                        ya_manager.size.width))

        #
        player_key = self.request.get('player_key')
        #
        game_key = self.request.get('game_key')

        #
        template = ya_manager.show_game(player_key, game_key)
        return self.response.out.write(template)
